<template>
	<!-- <view class="i-calendar" @touchmove.stop.prevent="clean"> -->
	<view class="i-calendar">
		<view v-if="!insert&&show" class="i-calendar__mask" :class="{'i-calendar--mask-show':aniMaskShow}" @click="clean"></view>
		<view v-if="insert || show" class="i-calendar__content" :class="{'i-calendar--fixed':!insert,'i-calendar--ani-show':aniMaskShow}">
			<view v-if="!insert" class="i-calendar__header i-calendar--fixed-top">
				<view class="i-calendar__header-btn-box" @click="close">
					<text class="i-calendar__header-text i-calendar--fixed-width">取消</text>
				</view>
				<view class="i-calendar__header-btn-box" @click="confirm">
					<text class="i-calendar__header-text i-calendar--fixed-width">确定</text>
				</view>
			</view>
			<view class="i-calendar__header" style="display:block;height:auto">
				<view class="flex justify-between align-center" style="height:50px">
					<text class="i-calendar__header-text text-bold">{{ (nowDate.month||'') +'月 ' + (nowDate.year||'') +'年'}}</text>
					<text class="i-calendar__backtoday" @click="backtoday">回到今天</text>
				</view>
			</view>
			<view class="i-calendar__box">
				<view v-if="showMonth" class="i-calendar__box-bg">
					<text class="i-calendar__box-bg-text">{{nowDate.month}}</text>
				</view>
				<view class="i-calendar__weeks">
					<view class="i-calendar__weeks-day">
						<text class="i-calendar__weeks-day-text">日</text>
					</view>
					<view class="i-calendar__weeks-day">
						<text class="i-calendar__weeks-day-text">一</text>
					</view>
					<view class="i-calendar__weeks-day">
						<text class="i-calendar__weeks-day-text">二</text>
					</view>
					<view class="i-calendar__weeks-day">
						<text class="i-calendar__weeks-day-text">三</text>
					</view>
					<view class="i-calendar__weeks-day">
						<text class="i-calendar__weeks-day-text">四</text>
					</view>
					<view class="i-calendar__weeks-day">
						<text class="i-calendar__weeks-day-text">五</text>
					</view>
					<view class="i-calendar__weeks-day">
						<text class="i-calendar__weeks-day-text">六</text>
					</view>
				</view>
				<view class="i-calendar__weeks" v-for="(item,weekIndex) in weeks" :key="weekIndex" v-show="!briefMode || inCurrentWeek(item)">
					<view class="i-calendar__weeks-item" v-for="(day,weeksIndex) in item" :key="weeksIndex">
						<i-calendar-item :weeks="day" :calendar="calendar" :selected="selected" :lunar="lunar" @change="choiceDate"></i-calendar-item>
					</view>
				</view>
			</view>
			<view @click="briefMode = !briefMode" class="flex align-center padding-lr" style="height:50px">
				<view v-show="!briefMode" class="i-calendar__header-btn-box" @click.stop="pre" style="width:auto">
					<button class="cu-btn bg-light-blue">上月</button>
				</view>

				<text :class="{'cuIcon-unfold': briefMode, 'cuIcon-fold': !briefMode}" style="margin-left:auto;margin-right:auto"></text>
				
				<view v-show="!briefMode" class="i-calendar__header-btn-box" @click.stop="next" style="width:auto">
					<button class="cu-btn bg-light-blue">下月</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Calendar from './util.js';
	import iCalendarItem from './i-calendar-item.vue'
	/**
	 * Calendar 日历
	 * @description 日历组件可以查看日期，选择任意范围内的日期，打点操作。常用场景如：酒店日期预订、火车机票选择购买日期、上下班打卡等
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=56
	 * @property {String} date 自定义当前时间，默认为今天
	 * @property {Boolean} lunar 显示农历
	 * @property {String} startDate 日期选择范围-开始日期
	 * @property {String} endDate 日期选择范围-结束日期
	 * @property {Boolean} range 范围选择
	 * @property {Boolean} insert = [true|false] 插入模式,默认为false
	 * 	@value true 弹窗模式
	 * 	@value false 插入模式
	 * @property {Array} selected 打点，期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
	 * @property {Boolean} showMonth 是否选择月份为背景
	 * @event {Function} change 日期改变，`insert :ture` 时生效
	 * @event {Function} confirm 确认选择`insert :false` 时生效
	 * @event {Function} monthSwitch 切换月份时触发
	 * @example <i-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
	 */
	export default {
		components: {
			iCalendarItem
		},
		props: {
			date: {
				type: String,
				default: ''
			},
			selected: {
				type: Array,
				default () {
					return []
				}
			},
			lunar: {
				type: Boolean,
				default: false
			},
			startDate: {
				type: String,
				default: ''
			},
			endDate: {
				type: String,
				default: ''
			},
			range: {
				type: Boolean,
				default: false
			},
			insert: {
				type: Boolean,
				default: true
			},
			showMonth: {
				type: Boolean,
				default: false
			},
			brief: {
				type: Boolean,
				default: false,
			},
		},
		data() {
			return {
				show: false,
				weeks: [],
				calendar: {},
				nowDate: '',
				aniMaskShow: false,
				briefMode: this.brief || false,
			}
		},
		watch: {
			selected(newVal) {
				this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
				this.weeks = this.cale.weeks
			},
			briefMode(bool) {
				if (bool) {
					this.cale = new Calendar({
						date: this.date,
						selected: this.selected,
						startDate: this.startDate,
						endDate: this.endDate,
						range: this.range,
					})
					this.init(this.cale.date.fullDate)
					this.setEmit('change')
				}
			},
		},
		created() {
			// 获取日历方法实例
			this.cale = new Calendar({
				date: this.date,
				selected: this.selected,
				startDate: this.startDate,
				endDate: this.endDate,
				range: this.range,
			})
			this.init(this.cale.date.fullDate)
		},
		methods: {
			inCurrentWeek(week) {
				return week.findIndex(d => d.fullDate === this.cale.date.fullDate) > -1
			},
			// 取消穿透
			clean() {},
			init(date) {
				this.weeks = this.cale.weeks
				this.nowDate = this.calendar = this.cale.getInfo(date)
			},
			open() {
				this.show = true
				this.$nextTick(() => {
					setTimeout(() => {
						this.aniMaskShow = true
					}, 50)
				})
			},
			close() {
				this.aniMaskShow = false
				this.$nextTick(() => {
					setTimeout(() => {
						this.show = false
					}, 300)
				})
			},
			confirm() {
				this.setEmit('confirm')
				this.close()
			},
			change() {
				if (!this.insert) return
				this.setEmit('change')
			},
			monthSwitch() {
				let {
					year,
					month
				} = this.nowDate
				this.$emit('monthSwitch', {
					year,
					month: Number(month)
				})
			},
			setEmit(name) {
				let {
					year,
					month,
					date,
					fullDate,
					lunar,
					extraInfo
				} = this.calendar
				this.$emit(name, {
					range: this.cale.multipleStatus,
					year,
					month,
					date,
					fulldate: fullDate,
					lunar,
					extraInfo: extraInfo || {}
				})
			},
			choiceDate(weeks) {
				if (weeks.disable) return
				this.calendar = weeks
				// 设置多选
				this.cale.setMultiple(this.calendar.fullDate)
				this.weeks = this.cale.weeks
				this.change()
			},
			backtoday() {
				this.cale.setDate(this.date)
				this.weeks = this.cale.weeks
				this.nowDate = this.calendar = this.cale.getInfo(this.date)
				this.change()
			},
			pre() {
				const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
				this.setDate(preDate)
				this.monthSwitch()

			},
			next() {
				const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
				this.setDate(nextDate)
				this.monthSwitch()
			},
			setDate(date) {
				this.cale.setDate(date)
				this.weeks = this.cale.weeks
				this.nowDate = this.cale.getInfo(date)
			}
		}
	}
</script>

<style scoped>
	.i-calendar {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
	}

	.i-calendar__mask {
		position: fixed;
		bottom: 0;
		top: 0;
		left: 0;
		right: 0;
		background-color: rgba(0, 0, 0, 0.4);
		transition-property: opacity;
		transition-duration: 0.3s;
		opacity: 0;
		/* #ifndef APP-NVUE */
		z-index: 99;
		/* #endif */
	}

	.i-calendar--mask-show {
		opacity: 1
	}

	.i-calendar--fixed {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		transition-property: transform;
		transition-duration: 0.3s;
		transform: translateY(460px);
		/* #ifndef APP-NVUE */
		z-index: 99;
		/* #endif */
	}

	.i-calendar--ani-show {
		transform: translateY(0);
	}

	.i-calendar__content {
		background-color: #fff;
	}

	.i-calendar__header {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 50px;
		border-bottom-color: #e5e5e5;
		border-bottom-style: solid;
		border-bottom-width: 1px;
	}

	.i-calendar--fixed-top {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		border-top-color: #e5e5e5;
		border-top-style: solid;
		border-top-width: 1px;
	}

	.i-calendar--fixed-width {
		width: 50px;
		/* padding: 0 15px;
 */
	}

	.i-calendar__backtoday {
		position: absolute;
		right: 0;
		top: 25rpx;
		padding: 0 5px;
		padding-left: 10px;
		height: 25px;
		line-height: 25px;
		font-size: 12px;
		border-top-left-radius: 25px;
		border-bottom-left-radius: 25px;
		color: #333;
		background-color: #f1f1f1;
	}

	.i-calendar__header-text {
		text-align: center;
		width: 100px;
		font-size: 28rpx;
		color: #333;
	}

	.i-calendar__header-btn-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 50px;
		height: 50px;
	}

	.i-calendar__header-btn {
		width: 10px;
		height: 10px;
		border-left-color: #808080;
		border-left-style: solid;
		border-left-width: 2px;
		border-top-color: #555555;
		border-top-style: solid;
		border-top-width: 2px;
	}

	.i-calendar--left {
		transform: rotate(-45deg);
	}

	.i-calendar--right {
		transform: rotate(135deg);
	}


	.i-calendar__weeks {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.i-calendar__weeks-item {
		flex: 1;

		position: relative;
	}

	.i-calendar__weeks-day {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 45px;
		border-bottom-color: #F5F5F5;
		border-bottom-style: solid;
		border-bottom-width: 1px;
	}

	.i-calendar__weeks-day-text {
		font-size: 14px;
	}

	.i-calendar__box {
		position: relative;
	}

	.i-calendar__box-bg {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
	}

	.i-calendar__box-bg-text {
		font-size: 200px;
		font-weight: bold;
		color: #999;
		opacity: 0.1;
		text-align: center;
		/* #ifndef APP-NVUE */
		line-height: 1;
		/* #endif */
	}
	.bg-light-blue {
		background: #e3f2fd;
	}
</style>